<template>
  <ve-card
    :title="'车牌号：' + record.licenseNo"
    :sub-title="record.eventStatusStr"
    @tap="handleDetail(record)"
    :type="getCardType(record)"
  >
    <!-- 内容 -->
    <OrderItemCell :record="record" />
    <!-- 操作 -->
    <ve-operate-list :operates="operates" :record="record" />
    <!-- 不可删除 -->
    <ve-confirm titleText="不支持删除" v-model="notDeleteVisible" cancelText="关闭" :showConfirm="false">
      <view>当违章状态为未处理，且关联的费用为未收/付款时，支持删除</view>
    </ve-confirm>

    <ve-modal title="删除违章单" v-model="deleteVisible" okText="删除" @confirm="onConfirmItem" @close="onCloseItem">
      <view class="modal-content">删除后，不支持恢复，是否确认删除？</view>
    </ve-modal>
  </ve-card>
</template>

<script setup>
  import OrderItemCell from './orderItemCell.vue'
  import { ref } from 'vue'
  import { addViolationTag, deleteViolation } from '@/api/asset/car-violation'

  defineProps({
    record: {
      type: Object,
    },
  })

  const notDeleteVisible = ref(false)
  const deleteVisible = ref(false)

  // 修改违章标签
  const customLabelFn = (record, value) => {
    addViolationTag({
      id: record.id,
      violationLabelIdList: value,
    })
      .then(() => {
        uni.showToast({
          title: '添加成功',
          icon: 'none',
        })
        // 刷新页面
        uni.$emit('refreshData')
      })
      .catch(() => {
        uni.showToast({
          title: '添加失败',
          icon: 'none',
        })
      })
  }

  const deleteRecord = ref()

  const operates = ref([
    {
      label: '处理完成',
      permission: 'svmsapp:car-violation.handle',
      show: (record) => record.eventStatus == 0,
      method: (record) => {
        uni.navigateTo({
          url: `/pages/asset/car-violation/components/add?origin=handle&id=${record.id}`,
        })
      },
    },
    {
      label: '确认/修改费用',
      permission: 'svmsapp:car-violation.fee',
      show: (record) => record.eventStatus !== 3 && record.violationFeeState !== 1 && record.handleType == 1,
      method: (record) => {
        uni.navigateTo({
          url: `/pages/asset/asset-fee/list?feeCategory=4&businesId=${record.id}&orderId=${record.orderId}&bizType=3`,
        })
      },
    },
    {
      label: '催缴',
      permission: 'svmsapp:car-violation.call',
      method: (record) => {
        uni.navigateTo({
          url: `/pages/asset/car-violation/reminder?id=${record.id}&cooperateHandleState=${record.cooperateHandleState}`,
        })
      },
    },
    {
      label: '行驶证记录',
      // 处理方式为【客户处理】【员工处理】，状态为未处理、已处理状态
      show: (record) => record.eventStatus !== 3 && record.violationFeeState == 1 && record.handleType !== 1,
      method: (record) => {
        uni.navigateTo({
          url: '/pages/asset/car-violation/driving-license?id=' + record.id,
        })
      },
    },
    {
      label: '添加标签',
      permission: 'svmsapp:car-violation.addTag',
      show: (record) => record.eventStatus !== 3,
      method: (record) => {
        uni.navigateTo({
          url: '/pages/utils/selector',
          events: {
            selectorConfirm: (value, label, option = {}) => {
              customLabelFn(record, value)
            },
          },
          success: (res) => {
            // 传递数据
            res.eventChannel.emit('selectorInit', {
              type: 'customLabelAdd',
              params: {},
              value: record.violationLabelIds?.split(',') || [],
              multiple: true,
              disabledValue: undefined,
              config: {},
              required: false,
            })
          },
        })
      },
    },
    {
      label: '编辑',
      permission: 'svmsapp:car-violation.edit',
      show: (record) => record.eventStatus == 0 || record.eventStatus == 1,
      method: (record) => {
        if (record.eventStatus == 1) {
          uni.navigateTo({
            url: `/pages/asset/car-violation/components/add?origin=finish&id=${record.id}`,
          })
        } else {
          uni.navigateTo({
            url: `/pages/asset/car-violation/components/add?origin=edit&id=${record.id}`,
          })
        }
      },
    },
    {
      label: '操作记录',
      permission: 'svmsapp:car-violation.logs',
      method: (record) => {
        uni.navigateTo({
          url: `/pages/utils/logs?id=${record.id}&type=5`,
        })
      },
    },
    {
      label: '删除',
      permission: 'svmsapp:car-violation.delete',
      method: (record) => {
        deleteRecord.value = record

        if (record.ifPay || record.eventStatus == 1) {
          notDeleteVisible.value = true
        } else {
          console.log('record', record)
          deleteVisible.value = true
        }
      },
    },
  ])

  const onConfirmItem = () => {
    deleteViolation({ id: deleteRecord.value.id }).then(() => {
      deleteVisible.value = false
      uni.showToast({
        title: '删除成功',
        icon: 'none',
      })
      uni.$emit('refreshData')
    })
  }

  const onCloseItem = () => {
    deleteRecord.value = null
  }

  const handleDetail = (record) => {
    const url = `/pages/asset/car-violation/detail/index?id=${record.id}`
    uni.navigateTo({
      url,
    })
  }

  const getCardType = (record) => {
    let result = 'gray'
    switch (record.eventStatus) {
      case 0:
        result = 'yellow'
        break
      case 1:
        result = 'gray'
        break
    }
    return result
  }
</script>

<style lang="less" scoped>
  .modal-content {
    text-align: center;
    padding-bottom: 20px;
  }
</style>
